package com.hyts.mvc.register;

import com.hyts.mvc.base.wrapper.ServletWrapper;
import com.hyts.mvc.bean.ServletRegistrationBean;
import com.hyts.mvc.config.MvcValue;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.Servlet;
import javax.servlet.ServletRegistration;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.register
 * author:Libo/Alex
 * create-date:2019-07-02 17:44
 * email:liboware@gmail.com
 * description:此类主要用于:servlet对象注册模式wrapper包装类
 */
@Builder
@Slf4j
public class ServletRegistationWrapper extends ServletWrapper {


    public void addServlet(String servletName,Class<? extends Servlet> servletClass){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(MvcValue.DEFAULT_VIEW_SERVLET_URL_PATH);
    }

    public void addServlet(String servletName,String servletClass,String... urlMapping){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(urlMapping);
    }

    public void addServlet(String servletName,Class<? extends Servlet> servletClass,int loadOnStartup){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(MvcValue.DEFAULT_VIEW_SERVLET_URL_PATH);
        // loadOnStartUp
        servlet.setLoadOnStartup(loadOnStartup);
    }

    public void addServlet(String servletName,Class<? extends Servlet> servletClass,int loadOnStartup,String... urlMapping){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(urlMapping);
        // loadOnStartUp
        servlet.setLoadOnStartup(loadOnStartup);
    }

    public void addServlet(String servletName,Class<? extends Servlet> servletClass,Map<String,String> initParameters,int loadOnStartup,String... urlMapping){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(urlMapping);
        // loadOnStartUp
        servlet.setLoadOnStartup(loadOnStartup);
        //init 初始化参数
        servlet.setInitParameters(initParameters);
    }

    public void addServlet(String servletName,Class<? extends Servlet> servletClass,Map<String,String> initParameters,boolean aync,String... urlMapping){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(urlMapping);
        //init 初始化参数
        servlet.setInitParameters(initParameters);
        //是否支持异步
        servlet.setAsyncSupported(aync);
    }

    public void addServlet(String servletName,Class<? extends Servlet> servletClass,Map<String,String> initParameters,boolean aync,int loadOnStartup,String... urlMapping){
        //注册组件  ServletRegistration
        ServletRegistration.Dynamic servlet = getServletContext().addServlet(servletName, servletClass);
        //配置servlet的映射信息
        servlet.addMapping(urlMapping);
        //init 初始化参数
        servlet.setInitParameters(initParameters);
        //是否支持异步
        servlet.setAsyncSupported(aync);
        // loadOnStartUp
        servlet.setLoadOnStartup(loadOnStartup);
    }

    @Override
    public void addServlet(ServletRegistrationBean servletRegistration) {
        addServlet(servletRegistration.getServletName(),servletRegistration.getServletClass(),servletRegistration.getInitParameters(),
                servletRegistration.isAsync(),servletRegistration.getLoadOnStartup(),servletRegistration.getUrlPaths());
    }

    @Override
    public void addServlet(List<ServletRegistrationBean> servletRegistrationList) {
        servletRegistrationList.parallelStream().forEach(this::addServlet);
    }
}
